package com.infocollect.project.grant.controller;

import java.util.Date;
import java.util.List;

import com.infocollect.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.infocollect.framework.aspectj.lang.annotation.Log;
import com.infocollect.framework.aspectj.lang.enums.BusinessType;
import com.infocollect.project.grant.domain.TappAuditAuxiliary;
import com.infocollect.project.grant.service.ITappAuditAuxiliaryService;
import com.infocollect.framework.web.controller.BaseController;
import com.infocollect.framework.web.domain.AjaxResult;
import com.infocollect.common.utils.poi.ExcelUtil;
import com.infocollect.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 审核辅助信息Controller
 * 
 * @author infocollect
 * @date 2022-11-06
 */
@RestController
@RequestMapping("/grant/auditauxiliary")
public class TappAuditAuxiliaryController extends BaseController
{
    @Autowired
    private ITappAuditAuxiliaryService tappAuditAuxiliaryService;

    /**
     * 查询审核辅助信息列表
     */
    @PreAuthorize("@ss.hasPermi('grant:auditauxiliary:list')")
    @GetMapping("/list")
    public TableDataInfo list(TappAuditAuxiliary tappAuditAuxiliary)
    {
        startPage();
        List<TappAuditAuxiliary> list = tappAuditAuxiliaryService.selectTappAuditAuxiliaryList(tappAuditAuxiliary);
        return getDataTable(list);
    }
    /**
     * 查询审核辅助信息列表
     */
    @GetMapping("/auditauxiliaryListall")
    public AjaxResult auditauxiliaryListall(TappAuditAuxiliary tappAuditAuxiliary)
    {
        if(StringUtils.isAnyEmpty(tappAuditAuxiliary.getTablename(),tappAuditAuxiliary.getZjhm())){
            return AjaxResult.error("错误的请求，没有表单信息或人员信息");
        }
        List<TappAuditAuxiliary> list = tappAuditAuxiliaryService.selectTappAuditAuxiliaryList(tappAuditAuxiliary);
        return AjaxResult.success(list);
    }
    /**
     * 导出审核辅助信息列表
     */
    @PreAuthorize("@ss.hasPermi('grant:auditauxiliary:export')")
    @Log(title = "审核辅助信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TappAuditAuxiliary tappAuditAuxiliary)
    {
        List<TappAuditAuxiliary> list = tappAuditAuxiliaryService.selectTappAuditAuxiliaryList(tappAuditAuxiliary);
        ExcelUtil<TappAuditAuxiliary> util = new ExcelUtil<TappAuditAuxiliary>(TappAuditAuxiliary.class);
        return util.exportExcel(list, "auditauxiliary");
    }

    /**
     * 获取审核辅助信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('grant:auditauxiliary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tappAuditAuxiliaryService.selectTappAuditAuxiliaryById(id));
    }

    /**
     * 新增审核辅助信息
     */
    @PreAuthorize("@ss.hasPermi('grant:auditauxiliary:add')")
    @Log(title = "审核辅助信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TappAuditAuxiliary tappAuditAuxiliary)
    {
        return toAjax(tappAuditAuxiliaryService.insertTappAuditAuxiliary(tappAuditAuxiliary));
    }

    /**
     * 修改审核辅助信息
     */
    @PreAuthorize("@ss.hasPermi('grant:auditauxiliary:edit')")
    @Log(title = "审核辅助信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TappAuditAuxiliary tappAuditAuxiliary)
    {
        return toAjax(tappAuditAuxiliaryService.updateTappAuditAuxiliary(tappAuditAuxiliary));
    }

    /**
     * 删除审核辅助信息
     */
    @PreAuthorize("@ss.hasPermi('grant:auditauxiliary:remove')")
    @Log(title = "审核辅助信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tappAuditAuxiliaryService.deleteTappAuditAuxiliaryByIds(ids));
    }
    /**
     * 辅助信息导入
     */
    @Log(title = "辅助信息导入")
    @PostMapping("/importAuditauxiliaryData")
    public AjaxResult importAuditauxiliaryData(@RequestParam("onlineFile") MultipartFile file, @RequestParam("tablename")String tablename) throws Exception {
        String annexName = file.getOriginalFilename();
        annexName = annexName.substring(0,annexName .lastIndexOf("."));
        ExcelUtil<TappAuditAuxiliary> util = new ExcelUtil<>(TappAuditAuxiliary.class);
        List<TappAuditAuxiliary> auditAuxiliaryList = util.importExcel(file.getInputStream());
        auditAuxiliaryList.stream().forEach(tappAuditAuxiliary -> {
            TappAuditAuxiliary check = new TappAuditAuxiliary();
            check.setTablename(tablename);
            check.setZjhm(tappAuditAuxiliary.getZjhm());
            tappAuditAuxiliary.setTablename(tablename);
            List<TappAuditAuxiliary> list = tappAuditAuxiliaryService.selectTappAuditAuxiliaryList(check);
            if(list.size()==0){
                tappAuditAuxiliaryService.insertTappAuditAuxiliary(tappAuditAuxiliary);
            }
        });
        return AjaxResult.success();
    }
}
